﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wq.Extensions
{
    /// <summary>
    /// Swagger 启动服务 
    /// </summary>
    public static class SwaggerSetup
    {
        public static void AddSwagger(this IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("manager", new OpenApiInfo { Title = "管理员接口", Version = "v1" });
                c.SwaggerDoc("meeting", new OpenApiInfo { Title = "会场接口", Version = "v1" });
                c.SwaggerDoc("case", new OpenApiInfo { Title = "实列接口", Version = "v1" });
                c.OrderActionsBy(x => x.RelativePath);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, Path.Combine(AppContext.BaseDirectory, "Wq.Manager.API.xml")), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, Path.Combine(AppContext.BaseDirectory, "Wq.Meeting.API.xml")), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, Path.Combine(AppContext.BaseDirectory, "Wq.Case.Api.xml")), true);

                c.OperationFilter<AddResponseHeadersFilter>();
                c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
                c.OperationFilter<SecurityRequirementsOperationFilter>();
                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
                {
                    Description = "请在输入时添加Bearer和一个空格",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey
                });
            });
        }

        public static void UseMySwagger(this IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/manager/swagger.json", "管理员接口");
                c.SwaggerEndpoint("/swagger/meeting/swagger.json", "会场接口");
                c.SwaggerEndpoint("/swagger/case/swagger.json", "实列接口");
            });
        }
    }
}
